package com.firmboy.zoo;

import org.apache.zookeeper.*;
import org.junit.Test;

import java.io.IOException;

/**
 * zookeeper的javaAPI练习
 *
 * @author firmboy
 * @create 2018-05-02 下午7:23
 **/
public class ZooTest {


    @Test
    public void zookLockTest() throws InterruptedException {

        ZooLock test = ZooLock.getInstance("test");

        String lock = test.getLock();

        Thread.sleep(10000L);

        System.out.println("释放锁");
        test.releaseLock(lock);

        while (true){
            if(0>1){
                break;
            }
        };


    }








    @Test
    public void zooTest() throws IOException, KeeperException, InterruptedException {

        ZooKeeper zk = new ZooKeeper("tcp.ittun.com:47203", 2000, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("已经出发了" + watchedEvent.getType() + "事件！");
            }
        });

        zk.create("/testRootPath","testRootData".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

        zk.create("/testRootPath/testChildPathOne","testChildPathOne".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);

        System.out.println(new String(zk.getData("/testRootPath",false,null)));

        System.out.println(zk.getChildren("/testRootPath",true));

        zk.setData("/testRootPath/testChildPathOne","modifyChildDataOne".getBytes(),-1);

        System.out.println("目录几点状态：["+zk.exists("/testRootPath",true)+"]");

        zk.create("/testRootPath/testChildPathTwo","testChildDataTwo".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);

        System.out.println(new String(zk.getData("/testRootPath/testChildPathTwo",true,null)));

        zk.delete("/testRootPath/testChildPathTwo",-1);

        zk.delete("/testRootPath/testChildPathOne",-1);

        zk.delete("/testRootPath",-1);

        zk.close();


    }

    @Test
    public void test2() throws IOException, KeeperException, InterruptedException {
        ZooKeeper zk = new ZooKeeper("tcp.ittun.com:47203", 2000, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("已经出发了" + watchedEvent.getType() + "事件！");
            }
        });

        zk.delete("/testRootPath",-1);

        zk.close();
    }

    @Test
    public void test() throws IOException, InterruptedException, KeeperException {
        ZooKeeper zk = new ZooKeeper("tcp.ittun.com:47203", 2000, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("已经出发了" + watchedEvent.getType() + "事件！");
            }
        });


        zk.create("/Locks",("lock_"+1).getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.EPHEMERAL_SEQUENTIAL);

        zk.create("/testRootPath","testRootData".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

        zk.getChildren("/Locks",false);


        while(true){
          if (0>1){
              break;
          }
        };


        zk.close();
    }

}
